package com.tudouji.project.permission.controller;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.flow.service.pojo.flow.FlowPermissionAssign;
import com.tudouji.project.organization.domain.Department;
import com.tudouji.project.system.domain.Dict;
import com.tudouji.project.system.mapper.DictMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.ISalespermissionService;
import com.tudouji.project.system.domain.SysUser;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.mapper.TuserMapper;
import com.tudouji.project.system.service.ITuserService;
import com.tudouji.project.system.service.impl.TuserServiceImpl;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 权限Controller
 * 
 * @author tudouji
 * @date 2021-04-23
 */
@RestController
@RequestMapping("/permission/salespermission")
public class SalespermissionController extends BaseController
{
    @Autowired
    private ISalespermissionService salespermissionService;
    
    @Autowired
	private TuserServiceImpl userService;
    @Autowired
	private DictMapper dictMapper;

    /**
     * 查询权限列表
     */
    @PreAuthorize("@ss.hasPermi('permission:salespermission:list')")
    @GetMapping("/list")
    public TableDataInfo list(Salespermission salespermission)
    {
        startPage();
        List<Salespermission> list = salespermissionService.selectSalespermissionList(salespermission);
        return getDataTable(list);
    }

    /**
     * 查询权限移交列表
     * @param salespermission
     * @return
     */
    @GetMapping("/transList")
    public TableDataInfo transList(Salespermission salespermission){
        List<Salespermission> list = salespermissionService.selectSalespermissionList(salespermission);
        return getDataTable(list);
    }


    /**
     * 导出权限列表
     */
    @PreAuthorize("@ss.hasPermi('permission:salespermission:export')")
    @Log(title = "权限", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Salespermission salespermission)
    {
        List<Salespermission> list = salespermissionService.selectSalespermissionList(salespermission);
        ExcelUtil<Salespermission> util = new ExcelUtil<Salespermission>(Salespermission.class);
        return util.exportExcel(list, "salespermission");
    }

    /**
     * 获取权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('permission:salespermission:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(salespermissionService.selectSalespermissionById(guid));
    }

    /**
     * 新增权限
     */
    @PreAuthorize("@ss.hasPermi('permission:salespermission:add')")
    @Log(title = "权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Salespermission salespermission)
    {
        return toAjax(salespermissionService.insertSalespermission(salespermission));
    }

    /**
     * 修改权限
     */
    @PreAuthorize("@ss.hasPermi('permission:salespermission:edit')")
    @Log(title = "权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Salespermission salespermission)
    {
        return toAjax(salespermissionService.updateSalespermission(salespermission));
    }

    /**
     * 删除权限
     */
    @PreAuthorize("@ss.hasPermi('permission:salespermission:remove')")
    @Log(title = "权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(salespermissionService.deleteSalespermissionByIds(guids));
    }
    
    /**
	 * 获取用户permission
	 * @return
	 */
    @GetMapping("/getPermis")
	public AjaxResult getPermisByUser() {
    	System.out.println("------------getPermis-----------");
    	SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    	System.out.println("-------------"+sysUser.getPhonenumber());
    	System.out.println("----------"+userService);
    	Tuser user = userService.selectTuserByName(sysUser.getPhonenumber());
    	if(user!=null){
    		Integer[] permis = new Integer[0];
    		Salespermission per=new Salespermission();
    		per.setUserguid(user.getGuid());
    		List<Salespermission> permissions=  salespermissionService.selectSalespermissionList(per);
    		if(permissions.size()>0){
    			Integer[] perms=new Integer[permissions.size()];
    			for(int i=0;i<permissions.size();i++){
    				perms[i]=permissions.get(i).getPermission();
    			}
    			List li = Arrays.asList(perms);
    			Set set = new HashSet(li);
    			permis = (Integer [])set.toArray(new Integer[0]);
    		}
    		return AjaxResult.success(permis);
    	}else{
    		return AjaxResult.error("没有权限");
    	}
		
	}

    /**
	 * 获取物流运费支付权限所包含的部门
	 * @return
	 */
    @GetMapping("/getDepsPermisByOrg")
	public AjaxResult getDepsPermisByOrg(String orgid, Integer permission) {
    	System.out.println("------------getDepsPermisByOrg-----------");
    	SysUser sysUser = SecurityUtils.getLoginUser().getUser();
    	Tuser user = userService.selectTuserByName(sysUser.getPhonenumber());
		Salespermission per=new Salespermission();
		per.setUserguid(user.getGuid());
		per.setOrgid(orgid);
		per.setPermission(permission);
		List<Department> permissions=  salespermissionService.getDepsPermisByOrg(per);
		return AjaxResult.success(permissions);
	}

    /**
	 * 获取物流运费支付权限所包含的部门
	 * @return
	 */
    @GetMapping("/findSalseUser")
	public AjaxResult findSalseUser() {
        AjaxResult result = AjaxResult.success();
        List<Tuser> users = userService.findSalseUser(new Tuser());
        List<Dict> scopeType = dictMapper.selectDictByType(30L);
        List<Dict> pdtype = dictMapper.selectDictByType(31L);
        result.put("users", users);
        result.put("scopeType", scopeType);
        result.put("pdtype", pdtype);
		return result;
	}


    /**
     * 权限移交
     */
    @Log(title = "权限", businessType = BusinessType.INSERT)
    @PostMapping("/usertransfer")
    public AjaxResult usertransfer(String transferType, Long userId, Long usertransferId, @RequestParam(required = false) Long permission)
    {
        return salespermissionService.usertransfer(transferType ,userId, usertransferId,permission);
    }

    /**
     * 权限移交
     * @param fromUserId
     * @param toUserId
     * @param picked
     * @return
     */
    @PostMapping("/trans")
    public AjaxResult trans(Long fromUserId,Long toUserId,String picked){
        return AjaxResult.success(salespermissionService.trans(fromUserId, toUserId, picked));
    }

    /**
     * 根据用户id查询用户权限
     * @param userguid
     * @param permission
     * @return
     */
    @GetMapping("")
    public AjaxResult selectPermissionByUserid(String userguid,Integer permission,Long deptId,String srguid,String orgid){
        return AjaxResult.success(salespermissionService.selectPermissionByUserid(userguid, permission,deptId,srguid,orgid));
    }

}

